﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;

namespace WinFormHostApp
{
    public partial class HostObjectApp : Form
    {
        public HostObjectApp()
        {
            InitializeComponent();
        }


        private void buttonLancer_Click(object sender, EventArgs e)
        {
                try
                {

                        // creation of a TCP-binary channel on port 12000
                        TcpChannel channel = new TcpChannel(12000);
                        // channel registration
                        ChannelServices.RegisterChannel(channel, true); //secure channel
                        if (checkBoxSingleton.Checked == true)
                        {
                            RemotingConfiguration.RegisterWellKnownServiceType(typeof(HostObject), "HostObject", WellKnownObjectMode.Singleton); // Singleton design pattern   
                            labelServer.Text = "Server is running with Singleton configuration";
                            labelServer.ForeColor = Color.Green;
                            checkBoxSingleCall.Enabled = false;
                            checkBoxSingleton.Enabled = false;
                            buttonLancer.Enabled = false;
                        }
                        else
                        {
                            RemotingConfiguration.RegisterWellKnownServiceType(typeof(HostObject), "HostObject", WellKnownObjectMode.SingleCall);
                            labelServer.Text = "Server is running with Single call configuration";
                            labelServer.ForeColor = Color.Green;
                            checkBoxSingleCall.Enabled = false;
                            checkBoxSingleton.Enabled = false;
                            buttonLancer.Enabled = false;
                        }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error creating/registering the channel : " + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
        }

        private void checkBoxSingleton_Click(object sender, EventArgs e)
        {
            checkBoxSingleCall.Checked = false;
            checkBoxSingleton.Checked = true;
        }

        private void checkBoxSingleCall_Click(object sender, EventArgs e)
        {
            checkBoxSingleCall.Checked = true;
            checkBoxSingleton.Checked = false;
        }
    }
}
